cmake_minimum_required(VERSION 3.5)

SET(RESTINIO restinio)

include(cmake/target.cmake)

project(${RESTINIO}) # not to misspell it.

add_library(${RESTINIO} INTERFACE)
add_library(restinio::restinio ALIAS ${RESTINIO})

IF (${CMAKE_VERSION} VERSION_LESS "3.8.0")
	set(CMAKE_CXX_STANDARD 14)
	set(CMAKE_CXX_STANDARD_REQUIRED ON)
	set(CMAKE_CXX_EXTENSIONS OFF)
ELSE ()
	target_compile_features(${RESTINIO} INTERFACE cxx_std_14)
ENDIF ()

get_filename_component(CURRENT_FILE_DIR ${CMAKE_CURRENT_LIST_FILE} DIRECTORY)
get_filename_component(CURRENT_FILE_DIR ${CURRENT_FILE_DIR} DIRECTORY)
SET(RESTINIO_INCLUDE_PATH ${CURRENT_FILE_DIR})
UNSET(CURRENT_FILE_DIR)

# Propagate include directories for RESTinio.
TARGET_INCLUDE_DIRECTORIES(${RESTINIO}
	INTERFACE
		$<BUILD_INTERFACE:${RESTINIO_INCLUDE_PATH}>
		$<INSTALL_INTERFACE:include>
	)

# ------------------------------------------------------------------------------
# ASIO or Boost::ASIO
IF ( RESTINIO_USE_BOOST_ASIO MATCHES "none" )
	IF (RESTINIO_STAND_ALONE_ASIO_HEADERS)
		TARGET_INCLUDE_DIRECTORIES(${RESTINIO} INTERFACE
			$<BUILD_INTERFACE:${RESTINIO_STAND_ALONE_ASIO_HEADERS}>)
	ENDIF ()

	IF( RESTINIO_STAND_ALONE_ASIO_DEFINES )
		TARGET_COMPILE_DEFINITIONS(${RESTINIO} INTERFACE ${RESTINIO_STAND_ALONE_ASIO_DEFINES})
	ENDIF ()

ELSE ()
	TARGET_INCLUDE_DIRECTORIES(${RESTINIO} INTERFACE ${Boost_INCLUDE_DIRS} )
	TARGET_COMPILE_DEFINITIONS(${RESTINIO} INTERFACE -DRESTINIO_USE_BOOST_ASIO )
ENDIF ()
# ------------------------------------------------------------------------------

IF (RESTINIO_FIND_DEPS)
	message(STATUS "RESTinio dependencies are included with `find_package()`")

	IF (RESTINIO_FMT_HEADER_ONLY)
		TARGET_LINK_LIBRARIES(${RESTINIO} INTERFACE fmt::fmt-header-only)
	ELSE ()
		TARGET_LINK_LIBRARIES(${RESTINIO} PUBLIC fmt::fmt)
	ENDIF ()

	IF (RESTINIO_USE_EXTERNAL_HTTP_PARSER)
		TARGET_LINK_LIBRARIES(${RESTINIO} INTERFACE http_parser)
	ELSE ()
		TARGET_LINK_LIBRARIES(${RESTINIO} INTERFACE unofficial::http_parser::http_parser)
	ENDIF ()
ELSE ()
	IF (RESTINIO_FMT_HEADER_ONLY)
		TARGET_LINK_LIBRARIES(${RESTINIO} INTERFACE $<BUILD_INTERFACE:fmt::fmt-header-only>)
	ELSE ()
		TARGET_LINK_LIBRARIES(${RESTINIO} INTERFACE $<BUILD_INTERFACE:fmt::fmt>)
	ENDIF ()

	IF (RESTINIO_USE_EXTERNAL_HTTP_PARSER)
		TARGET_LINK_LIBRARIES(${RESTINIO} INTERFACE $<BUILD_INTERFACE:http_parser>)
	ELSE ()
		TARGET_LINK_LIBRARIES(${RESTINIO} INTERFACE $<BUILD_INTERFACE:unofficial::http_parser::http_parser>)
	ENDIF ()
ENDIF ()

IF (RESTINIO_USE_EXTERNAL_EXPECTED_LITE)
	TARGET_LINK_LIBRARIES(${RESTINIO} INTERFACE nonstd::expected-lite)
	TARGET_COMPILE_DEFINITIONS(${RESTINIO}
		INTERFACE -DRESTINIO_EXTERNAL_EXPECTED_LITE)
ENDIF()

IF (RESTINIO_USE_EXTERNAL_OPTIONAL_LITE)
	TARGET_LINK_LIBRARIES(${RESTINIO} INTERFACE nonstd::optional-lite)
	TARGET_COMPILE_DEFINITIONS(${RESTINIO}
		INTERFACE -DRESTINIO_EXTERNAL_OPTIONAL_LITE)
ENDIF()

IF (RESTINIO_USE_EXTERNAL_STRING_VIEW_LITE)
	TARGET_LINK_LIBRARIES(${RESTINIO} INTERFACE nonstd::string-view-lite)
	TARGET_COMPILE_DEFINITIONS(${RESTINIO}
		INTERFACE -DRESTINIO_EXTERNAL_STRING_VIEW_LITE)
ENDIF()

IF (RESTINIO_USE_EXTERNAL_VARIANT_LITE)
	TARGET_LINK_LIBRARIES(${RESTINIO} INTERFACE nonstd::variant-lite)
	TARGET_COMPILE_DEFINITIONS(${RESTINIO}
		INTERFACE -DRESTINIO_EXTERNAL_VARIANT_LITE)
ENDIF()

IF ( NOT (RESTINIO_USE_BOOST_ASIO STREQUAL "none") )
	TARGET_LINK_LIBRARIES(${RESTINIO} INTERFACE ${Boost_SYSTEM_LIBRARY} )
ENDIF ()

FILE(GLOB_RECURSE RESTINIO_HEADERS_ALL RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.?pp)

# If *_lite library is used as external dependency then the corresponding
# 3rd-party file should be removed.
IF (RESTINIO_USE_EXTERNAL_EXPECTED_LITE)
	list(REMOVE_ITEM RESTINIO_HEADERS_ALL third_party/expected-lite/expected.hpp)
ENDIF()

IF (RESTINIO_USE_EXTERNAL_OPTIONAL_LITE)
	list(REMOVE_ITEM RESTINIO_HEADERS_ALL third_party/optional-lite/optional.hpp)
ENDIF()

IF (RESTINIO_USE_EXTERNAL_STRING_VIEW_LITE)
	list(REMOVE_ITEM RESTINIO_HEADERS_ALL third_party/string-view-lite/string_view.hpp)
ENDIF()

IF (RESTINIO_USE_EXTERNAL_VARIANT_LITE)
	list(REMOVE_ITEM RESTINIO_HEADERS_ALL third_party/variant-lite/variant.hpp)
ENDIF()

IF (RESTINIO_INSTALL)
	include(GNUInstallDirs)
	include(CMakePackageConfigHelpers)

	SET(RESTINIO_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/restinio CACHE STRING
		"Installation directory for cmake files, relative to ${CMAKE_INSTALL_PREFIX}.")
	SET(RESTINIO_VERSION_CONFIG ${PROJECT_BINARY_DIR}/restinio-config-version.cmake)
	SET(RESTINIO_PROJECT_CONFIG ${PROJECT_BINARY_DIR}/restinio-config.cmake)

	SET(RESTINIO_INC_DIR ${CMAKE_INSTALL_INCLUDEDIR}/restinio/ CACHE STRING
		"Installation directory for include files, relative to ${CMAKE_INSTALL_PREFIX}.")

	# Generate the version, config and target files into the build directory.
	write_basic_package_version_file(
		${RESTINIO_VERSION_CONFIG}
		VERSION ${RESTINIO_VERSION}
		COMPATIBILITY ExactVersion )

	configure_package_config_file(
		"cmake/restinio-config.cmake.in"
		${RESTINIO_PROJECT_CONFIG}
		INSTALL_DESTINATION ${RESTINIO_CMAKE_DIR}
		PATH_VARS RESTINIO_VERSION)

	# Install version, config and target files.
	install(
		FILES ${RESTINIO_PROJECT_CONFIG} ${RESTINIO_VERSION_CONFIG}
		DESTINATION ${RESTINIO_CMAKE_DIR})

	SET(RESTINIO_INSTALL_TARGETS ${RESTINIO})

	install(
		TARGETS ${RESTINIO_INSTALL_TARGETS}
		EXPORT RESTINIO_ALL_TARGETS
		LIBRARY DESTINATION lib
		ARCHIVE DESTINATION lib
		RUNTIME DESTINATION bin
	)

	install(
		EXPORT RESTINIO_ALL_TARGETS
		NAMESPACE restinio::
		FILE restinio-targets.cmake
		DESTINATION ${RESTINIO_CMAKE_DIR} )

	FOREACH (HEADER_FILE ${RESTINIO_HEADERS_ALL} )
		get_filename_component( DIR ${HEADER_FILE} PATH )
		install( FILES ${HEADER_FILE} DESTINATION ${RESTINIO_INC_DIR}/${DIR} )
	ENDFOREACH ()

ENDIF ()
